package net.pulseproject.router.debuggerapp.beatsim.prototype;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.WindowConstants;

public class FirstPrototype {
	
	private static final BeatVisualizer visualizer = new BeatVisualizer();
	private static Timer timer = new Timer();
	private static boolean isRunning = false;
	private static JScrollPane scroll;
	
	public static void main(final String[] args) {
		EventQueue.invokeLater(new Runnable() {
			@Override public void run() {
				final JFrame frame = new JFrame();
				frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
				frame.getContentPane().add(FirstPrototype.createMainPanel());
//				frame.setSize(800, 600);
				frame.pack();
				frame.setVisible(true);
			}
		});
	}
	private static int countsTask = 0;
	
	private static void onBtnClicked() {
		System.out.println("onBtnClicked() isRunning=" + isRunning);
		if(isRunning) {
			timer.cancel();
			timer = new Timer();
		} else {
			timer.schedule(new TimerTask() {
				@Override
				public void run() {
					countsTask++;
					System.out.println("task.run() visualizer.time=" + visualizer.time);
//					visualizer.time += 50;
					visualizer.repaint();
					if(countsTask == 3) {
						visualizer.execDrawVert = true;
						countsTask = 1;
					}
				}
			}, 0, 100);
		}
		isRunning = !isRunning;
	}
	
	private static Component createMainPanel() {
		final JPanel panel = new JPanel(new BorderLayout());
		final JButton b = new JButton("config");
		b.addActionListener(new ActionListener() {
			@Override public void actionPerformed(ActionEvent actionevent) {
				onBtnClicked();
			}
		});
		panel.add(b, BorderLayout.WEST);
		
		scroll = new JScrollPane(visualizer);
		scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
		final Dimension d = new Dimension(400, 100);
		scroll.setSize(d);
		scroll.setPreferredSize(d);
		scroll.setMaximumSize(d);
		scroll.setMinimumSize(d);
		panel.add(scroll, BorderLayout.CENTER);
		
		return panel;
	}

	static class BeatVisualizer extends JPanel {
		private static final long serialVersionUID = 4171706265387997934L;
		public int time = 0; // TODO synchronized
		public boolean execDrawVert;
		public BeatVisualizer() {
			this.setBackground(Color.YELLOW);
		}
		@Override
		public void paintComponent(final Graphics g) {
//			System.out.println("paintComponent() time=" + time);
////			final Graphics2D graphics = (Graphics2D) g;
////	        graphics.drawString("Using Java 2D API to draw a String 2D API to draw a String 2D API to draw a String 2D API to draw a String 2D API to draw a String", 20, 100);
//			super.paintComponent(g);
//		    this.setSize(50, time + 10);
		    
			Graphics2D g2 = (Graphics2D) g;
//            Dimension d = getSize();
//            g2.setBackground(getBackground());
//            g2.clearRect(0, 0, d.width, d.height);
			
            System.out.println("getBounds(): " + getBounds());
            Color rc = randColor();
            g2.setColor(rc);
            g2.fillRect(this.time, 0, this.time + 50, 5);
//            g2.drawLine(time, 0, time + 50, 0); // x1, y1, x2, y2
            
            if(this.execDrawVert) {
            	System.out.println("drawing vert");
            	g2.setColor(rc.brighter());
            	g2.fillRect(this.time, 5, this.time + 50, 30);
            	this.execDrawVert = false;
            }
            
            this.time += 50;
            
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                                RenderingHints.VALUE_ANTIALIAS_ON);

		}
		private static final Random R = new Random();
		private static Color randColor() {
			return new Color(R.nextInt(250), R.nextInt(250), R.nextInt(250));
		}
	}
}
